package com.lcredu.sqlSession;

import com.lcredu.config.MapperBean;
import com.lcredu.entity.Monster;
import com.lcredu.mapper.MonsterMapper;
import com.lcredu.sqlSeesion.LcrConfiguration;
import com.lcredu.sqlSeesion.LcrExecutor;
import com.lcredu.sqlSeesion.LcrSqlSession;
import com.lcredu.sqlSeesion.LcrSqlSessionFactory;
import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author 黎长容
 * @date 2025-07-28 16:22
 */
public class Configuration {
    
    @Test
    public void build() throws SQLException {
        LcrConfiguration lcrConfiguration = new LcrConfiguration();
        Connection connection = lcrConfiguration.build("lcr_mybatis.xml");
        PreparedStatement preparedStatement = connection.prepareStatement("select * from monster where id = ?");
        preparedStatement.setInt(1,1);
        ResultSet resultSet = preparedStatement.executeQuery();
        Monster monster = new Monster();
        //这里使用反射,但是简化
        while (resultSet.next()) {
            monster.setId(resultSet.getInt("id"));
            monster.setAge(resultSet.getInt("age"));
            monster.setBirthday(resultSet.getDate("birthday"));
            monster.setEmail(resultSet.getString("email"));
            monster.setGender(resultSet.getInt("gender"));
            monster.setName(resultSet.getString("name"));
            monster.setSalary(resultSet.getDouble("salary"));
        }
        System.out.println(monster);


    }
    
    
    @Test
    public void queryById(){
        LcrExecutor lcrExecutor = new LcrExecutor();
        String sql = "select * from monster where id = ?";
        Monster monster = lcrExecutor.queryById(sql, 1);
        System.out.println(monster);
        
    }
    
    @Test
    public void selectOne(){
        LcrSqlSession lcrSqlSession = new LcrSqlSession();
        String sql = "select * from monster where id = ?";
        Object object = lcrSqlSession.selectOne(sql, 1);
        System.out.println(object.getClass());
    }
    
    @Test
    public void readMapper(){
        LcrConfiguration lcrConfiguration = new LcrConfiguration();
        MapperBean mapperBean = lcrConfiguration.readMapper("MonsterMapper.xml");
        System.out.println(mapperBean);
        System.out.println("ok...");
    }
    
    @Test
    public void getMapper(){
        LcrSqlSession lcrSqlSession = new LcrSqlSessionFactory().openSqlSession();
        MonsterMapper mapper = lcrSqlSession.getMapper(MonsterMapper.class);
        Monster monster = mapper.getMonsterById(1);
        System.out.println(monster);
    }
}
